<script lang="ts" setup>
import { ref } from 'vue'
import { ElTable } from 'element-plus'
import { GetUserData } from '@/api/modules/birthday'
import PageNumber from '@/store/modules/PageNumberCache'

const findFrom = ref({
  ActBirthId: '',
  UserId: '',
  SCOpenId: '',
  UserPhone: '',
  ScoreTrano: '',
  CouponId: '',
  CouponName: '',
})
const dataList: any = reactive({
  list: [],
  option: [],
})
const loading = ref(true)
const total = ref(0)
let PageSize = 20
const PageNumberCache = PageNumber()

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.ActBirthId && ({ ActBirthId: findFrom.value.ActBirthId })),
    ...(findFrom.value.UserId && ({ UserId: findFrom.value.UserId })),
    ...(findFrom.value.SCOpenId && ({ SCOpenId: findFrom.value.SCOpenId })),
    ...(findFrom.value.UserPhone && ({ UserPhone: findFrom.value.UserPhone })),
    ...(findFrom.value.ScoreTrano !== '' && ({ ScoreTrano: findFrom.value.ScoreTrano })),
    ...(findFrom.value.CouponId !== '' && ({ CouponId: findFrom.value.CouponId })),
    ...(findFrom.value.CouponName && ({ CouponName: findFrom.value.CouponName })),
  }
  await GetUserData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}

// 查询
function Inquire() {
  GetDataList(1, PageSize)
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}
function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
  PageNumberCache.SetBirthdayUserPage(val)
}

const nowPageNum = computed(() => {
  return PageNumberCache.GetBirthdayUserPage()
})

onMounted(() => {
  GetDataList(PageNumberCache.GetBirthdayUserPage())
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="生日活动ID">
                <el-input v-model="findFrom.ActBirthId" style="width: 300px;" placeholder="请输入生日活动ID" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="用户ID">
                <el-input v-model="findFrom.UserId" style="width: 300px;" placeholder="请输入用户ID" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="用户OpenId">
                <el-input v-model="findFrom.SCOpenId" style="width: 300px;" placeholder="请输入用户OpenId" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="手机号">
                <el-input v-model="findFrom.UserPhone" style="width: 300px;" placeholder="请输入手机号" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="流水号">
                <el-input v-model="findFrom.ScoreTrano" style="width: 300px;" placeholder="请输入流水号" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="券ID">
                <el-input v-model="findFrom.CouponId" style="width: 300px;" placeholder="请输入券ID" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="券名称">
                <el-input v-model="findFrom.CouponName" style="width: 300px;" placeholder="请输入券名称" @keyup.enter="Inquire" />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <ElTable v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="100" />
        <el-table-column align="center" prop="actBirthId" label="生日礼活动ID" width="120" />
        <el-table-column align="center" prop="subscriberInfo.userId" label="用户ID" width="100" />
        <el-table-column align="center" prop="subscriberInfo.scOpenId" label="OpenID" width="280" />
        <el-table-column align="center" prop="subscriberInfo" label="用户信息" width="200">
          <template #default="scope">
            <div v-if="!scope.row.subscriberInfo">
              未授权
            </div>
            <div v-else class="user-name">
              <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.subscriberInfo?.userAvatar" />
              <div>
                <p style="margin: 0;">
                  {{ scope.row.subscriberInfo?.userNickName }}
                </p>
                <span>
                  {{ scope.row.subscriberInfo?.userPhone }}
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="couponInfo.id" label="券ID" width="100" />
        <el-table-column align="center" prop="couponInfo.name" label="券名称" width="200" />
        <el-table-column align="center" prop="couponInfo.price" label="券面值" width="150" />
        <el-table-column align="center" prop="couponInfo" label="券类型" width="100">
          <template #default="scope">
            <el-tag type="primary">
              {{ scope.row.couponInfo?.type === 1 ? '赠品券'
                : scope.row.couponInfo?.type === 2 ? '奖品券'
                  : scope.row.couponInfo?.type === 3 ? '满减券' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="couponInfo.productCode" label="商品编码" width="200" />
        <el-table-column align="center" prop="addTime" label="申请时间" width="200" />
        <el-table-column align="center" prop="updateTime" label="修改时间" width="200" />
        <!-- <el-table-column align="center" label="操作" fixed="right" width="300">
          <template #default="scope">
            <el-button @click="freetrialInvite(scope.row.id)">
              查看邀请记录
            </el-button>
            <el-button @click="freetrialevaluation(scope.row.id)">
              查看评测记录
            </el-button>
          </template>
        </el-table-column> -->
      </ElTable>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;

  div {
    text-align: left;
  }
}
</style>
